package com.jt.tenant.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetInsQuote;
import com.jt.tenant.widget.MyListView;
import com.jt.tenant.widget.Tools;

import java.util.List;

/**
 * Created by Administrator on 2016/4/7.
 */
public class InsOfferAdapter extends BaseAdapter {

    private Context context;

    private List<GetInsQuote.Quote> list;

    private InsOfferLevel2Adapter adapter;

    private double total;

    private Refrence refrence;

    public InsOfferAdapter(Context context, List<GetInsQuote.Quote> list, Refrence refrence) {

        this.context = context;

        this.list = list;

        this.refrence = refrence;

    }

    @Override
    public int getCount() {

        return list.size();

    }

    @Override
    public Object getItem(int position) {

        return list.get(position);

    }

    @Override
    public long getItemId(int position) {

        return position;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_ins_offer_1, null);

            JTApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);

            holder = new ViewHolder();

            holder.quote_title = (LinearLayout)convertView.findViewById(R.id.lin_ins_quote_title);

            holder.name = (TextView) convertView.findViewById(R.id.tv_ins_quote_name);

            holder.check = (ImageView) convertView.findViewById(R.id.iv_ins_quote_check);

            holder.detail = (LinearLayout)convertView.findViewById(R.id.lin_ins_quote_detail);

            holder.ins_quote_list = (MyListView) convertView.findViewById(R.id.lv_ins_quote_2);

            holder.lin_tci_starttime = (LinearLayout)convertView.findViewById(R.id.lin_ins_quote_tci_starttime);

            holder.tv_tci_starttime = (TextView)convertView.findViewById(R.id.tv_ins_quote_tci_starttime);

            holder.lin_vci_starttime = (LinearLayout)convertView.findViewById(R.id.lin_ins_quote_vci_starttime);

            holder.tv_vci_starttime = (TextView)convertView.findViewById(R.id.tv_ins_quote_vci_starttime);

            holder.lin_vci_endtime = (LinearLayout)convertView.findViewById(R.id.lin_ins_quote_vci_endtime);

            holder.tv_vci_endtime = (TextView)convertView.findViewById(R.id.tv_ins_quote_vci_endtime);

            holder.lin_vci_count = (LinearLayout)convertView.findViewById(R.id.lin_ins_quote_vci_count);

            holder.tv_vci_count = (TextView)convertView.findViewById(R.id.tv_ins_quote_vci_count);

            holder.tv_price = (TextView)convertView.findViewById(R.id.tv_ins_quote_reference) ;

            holder.tv_price_true = (TextView)convertView.findViewById(R.id.tv_ins_quote_preferential) ;

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }


        holder.name.setText(list.get(position).title);

        total = 0;

        for(int i = 0;i<list.get(position).quoteItemList.size();i++){

            total  += (Float.parseFloat(list.get(position).quoteItemList.get(i).price)+Float.parseFloat(list.get(position).quoteItemList.get(i).flag_money));

        }

        if(list.get(position).check == 0 ){

            holder.check.setImageResource(R.mipmap.icon_pink_uncheck);

        }else {

            holder.check.setImageResource(R.mipmap.icon_pink_check);

        }

        if(list.get(position).flag == 0){

            holder.detail.setVisibility(View.GONE);

        }else {

            holder.detail.setVisibility(View.VISIBLE);

            holder.ins_quote_list.setAdapter(adapter = new InsOfferLevel2Adapter(context,list.get(position).quoteItemList));

            adapter.notifyDataSetChanged();

        }

//        holder.quote_title.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                refrence.refrence(position,0);
//
//            }
//        });

        holder.check.setVisibility(View.GONE);

//        holder.check.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                refrence.refrence(position,1);
//
//            }
//        });

        double price = 0;

        for(int i = 0;i<list.get(position).quoteItemList.size();i++){

            price += Double.parseDouble(list.get(position).quoteItemList.get(i).price);

            price += Double.parseDouble(list.get(position).quoteItemList.get(i).flag_money);

        }

        holder.tv_price.setText(Tools.F_num(String.valueOf(price))+"元");

        holder.tv_price_true.setText(Tools.F_num(String.valueOf(price))+"元");

        return convertView;

    }

    class ViewHolder {

        private LinearLayout quote_title;//lin_ins_quote_title

        private TextView name;//tv_ins_quote_name

        private ImageView check;//iv_ins_quote_check

        private LinearLayout detail;//lin_ins_quote_detail

        private MyListView ins_quote_list;//lv_ins_quote_2

        private LinearLayout lin_tci_starttime;

        private TextView tv_tci_starttime;

        private LinearLayout lin_vci_starttime;

        private TextView tv_vci_starttime;

        private LinearLayout lin_vci_endtime;

        private TextView tv_vci_endtime;

        private LinearLayout lin_vci_count;

        private TextView tv_vci_count;

        private TextView tv_price;

        private TextView tv_price_true;



    }

    public interface Refrence{

        public void refrence(int position,int type);

    }
}
